package com.xinfu.newproject.ui.login_register;

import android.os.CountDownTimer;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.xinfu.newproject.R;
import com.xinfu.newproject.activity.LoginActivity;
import com.xinfu.newproject.base.BaseFragment;
import com.xinfu.newproject.bean.login.LoginBean;
import com.xinfu.newproject.presenter.RegisterPresenter;
import com.xinfu.newproject.presenter.SMSCodePresenter;
import com.xinfu.newproject.util.BToast;
import com.xinfu.newproject.util.LoginUtils;
import com.xinfu.newproject.util.SPUtils;
import com.xinfu.newproject.view.IView;

import net.qiujuer.genius.widget.GeniusEditText;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

import static com.xinfu.newproject.contants.Constant.LOGIN;

public class RegisterFragment extends BaseFragment implements IView.IRegisterView, IView.ISMSCodeView {

    @BindView(R.id.head_text)
    TextView titleTv;

    @BindView(R.id.register_code_edt)
    GeniusEditText codeEt;

    @BindView(R.id.register_pwd_edt)
    GeniusEditText pwdEt;

    @BindView(R.id.register_look_iv)
    ImageView lookIv;

    @BindView(R.id.register_get_code_tv)
    TextView getCodeTv;

    @BindView(R.id.fragment_register_phone_tv)
    TextView phoneTv;

    @BindView(R.id.register_pwd_edt_hint)
    TextView pwd_hint_tv;

    @BindView(R.id.register_code_edt_bg)
    View codeView;

    @BindView(R.id.register_pwd_edt_bg)
    View pwdView;

    protected BackHandledInterface mBackHandledInterface;
    private boolean mIsPasswordLook = false;

    @BindView(R.id.register_go)
    Button goBt;

    private RegisterPresenter registerPresenter = new RegisterPresenter(this);
    private SMSCodePresenter smsCodePresenter = new SMSCodePresenter(this);
    private Disposable mDisposable; //用于Activity销毁时停止执行

    private MyCountDownTimer myCountDownTimer;

    private LoginUtils loginUtils;

    private boolean mIsPasswordRight = false;

    @Override
    protected int setLayoutResourceID() {
        return R.layout.fragment_register;
    }

    @Override
    protected void setUpView() {
        titleTv.setText("");
        refresh();
        loginUtils = new LoginUtils(getMContext());
        loginUtils.setPasswordListener(pwdEt, pwd_hint_tv, pwdView, lookIv, new LoginUtils.EditPhoneListener() {
            @Override
            public void isPhoneRight(boolean bool) {
                mIsPasswordRight = bool;
                isLogin();
            }
        });

        myCountDownTimer = new MyCountDownTimer(60000, 1000);
    }

    private void isLogin() {

        if (mIsPasswordRight) {
            goBt.setEnabled(true);
            goBt.setBackgroundResource(R.drawable.business_time_bg);
            goBt.setTextColor(getResources().getColor(R.color.colorWhite));
        } else {
            goBt.setEnabled(false);
            goBt.setBackgroundResource(R.drawable.gray_background);
            goBt.setTextColor(getResources().getColor(R.color.location_color));
        }
    }

    @Override
    protected void setUpData() {

    }

    @OnClick({R.id.register_go, R.id.register_get_code_tv, R.id.head_back, R.id.register_look_iv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.register_go:
                registerPresenter.register();
                break;
            case R.id.register_get_code_tv:
                smsCodePresenter.getSMSCode();
                myCountDownTimer.start();
                break;
            case R.id.head_back:
                getActivity().finish();
                break;
            case R.id.register_look_iv:
                if (mIsPasswordLook) {
                    //显示为密码****
                    pwdEt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    lookIv.setImageResource(R.mipmap.invisible_icon);
                } else {
                    //显示为文本
                    pwdEt.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    lookIv.setImageResource(R.mipmap.visible_icon);
                }
                pwdEt.setSelection(pwdEt.getText().length());
                mIsPasswordLook = !mIsPasswordLook;
                break;
        }
    }

    private void refresh() {

        phoneTv.setText(getArguments().getString("respose"));
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            refresh();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
    }

    @Override
    public String getUserName() {
        return phoneTv.getText().toString().trim();
    }

    @Override
    public String getSMSCode() {
        return codeEt.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return pwdEt.getText().toString().trim();
    }

    @Override
    public void showToast(String content) {
        BToast.showText(getMContext(), content);
    }

    @Override
    public void closeDispose(Disposable disposable) {
        this.mDisposable = disposable;
    }

    @Override
    public void showProgress() {
        ((LoginActivity) getMContext()).showLoding();
    }

    @Override
    public void hideProgress() {
        ((LoginActivity) getMContext()).hideLoding();
    }

    @Override
    public void getMsg(String codeMsg) {
        Log.e("testSMSCode", codeMsg);
    }

    @Override
    public void toOtherActivity(int fragment, LoginBean.ResultBean resultBean) {
        SPUtils.put(getMContext(), LOGIN, new Gson().toJson(resultBean));
        mBackHandledInterface = (BackHandledInterface) getMContext();
        mBackHandledInterface.setSelectedFragment(fragment, phoneTv.getText().toString().trim());
    }

    //倒计时函数
    private class MyCountDownTimer extends CountDownTimer {

        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        //计时过程
        @Override
        public void onTick(long l) {
            if (getCodeTv != null) {
                //防止计时过程中重复点击
                getCodeTv.setClickable(false);
                getCodeTv.setText(l / 1000 + "秒");
            }
        }

        //计时完毕的方法
        @Override
        public void onFinish() {
            if (getCodeTv != null) {
                //重新给Button设置文字
                getCodeTv.setText("重新获取");
                //设置可点击
                getCodeTv.setClickable(true);
            }
        }
    }

}
